home *** CD-ROM | disk | FTP | other *** search
- unit DBErrorU;
-
- interface
-
- uses
- SysUtils, Windows, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, Grids, DBGrids, DB, DBTables, StdCtrls, ExtCtrls;
-
- type
- TForm1 = class(TForm)
- DataSource1: TDataSource;
- Table1: TTable;
- DBGrid1: TDBGrid;
- Panel1: TPanel;
- Button1: TButton;
- Button2: TButton;
- procedure FormCreate(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- procedure Button2Click(Sender: TObject);
- public
- procedure DoException(Sender: TObject; E: Exception);
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- uses
- BDE, DBExcept;
-
- {$R *.DFM}
-
- procedure TForm1.DoException(Sender: TObject; E: Exception);
- begin
- if E is EAccessViolation then
- //Simple reaction to an Access Violation
- ShowMessage('Ooch, Ouch, Ow!! That hurt!')
- else
- //Deal with all other exceptions in the normal way
- Application.ShowException(E);
- end;
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- //Set up personal default exception handler first
- Application.OnException := DoException;
- //Now create error dialog
- DbEngineErrorDlg := TDbEngineErrorDlg.Create(Application);
- //And install automatic support for it
- DbEngineErrorDlg.HookExceptions
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- //Generate an Access Violation
- IntToStr(PInteger(nil)^)
- end;
-
- procedure TForm1.Button2Click(Sender: TObject);
- begin
- try
- //Make sure we are not editing
- Table1.Cancel;
- //Record an existing unique key value
- Tag := Table1['CustNo'];
- //Add new record
- Table1.Insert;
- //Use same unique value
- Table1['CustNo'] := Tag;
- //Try and save, giving a key violation error
- Table1.Post
- except
- on E: EDBEngineError do
- begin
- //Ask error form to deal with exception
- DbEngineErrorDlg.ShowException(E);
- Table1.Cancel;
- end
- end
- end;
-
- end.
-